package cn.com.utils;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;

public class MinioService {

    private MinioClient minioClient;

    public MinioService(String endpoint, String accessKey, String secretKey) {
        minioClient = MinioClient.builder()
                                 .endpoint(endpoint)
                                 .credentials(accessKey, secretKey)
                                 .build();
    }

    // 创建桶
    public void createBucket(String bucketName) throws Exception {
        boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!isExist) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    // 设置桶的访问策略
    public void setBucketPolicy(String bucketName, String policyJson) throws Exception {
        minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policyJson).build());
    }
}
